<?php

namespace App\Http\Controllers\Admin\V1;

use Illuminate\Http\JsonResponse;
use App\Extensions\Log\V1\InstanceLog;
use App\Services\Admin\V1\AdminService;
use App\Exceptions\Admin\V1\AdminException;
use App\Http\Resources\Admin\V1\MenuResource;
use Illuminate\Contracts\Auth\Authenticatable;
use App\Http\Resources\Admin\V1\AdminResource;
use App\Http\Resources\Admin\V1\AdminCollection;
use App\Http\Requests\Admin\V1\AdminShowRequest;
use App\Http\Requests\Admin\V1\AdminStoreRequest;
use App\Http\Requests\Admin\V1\AdminIndexRequest;
use App\Http\Requests\Admin\V1\AdminUpdateRequest;
use App\Http\Requests\Admin\V1\AdminDestroyRequest;
use Illuminate\Database\Eloquent\ModelNotFoundException;

// 控制器编号02
class AdminController extends Controller
{
    /**
     * @var AdminService
     */
    private $admin_service;
    /**
     * @var Authenticatable|null
     */
    private $user_info;

    /**
     * AdminController constructor.
     * @param AdminService $adminService
     */
    public function __construct(AdminService $adminService)
    {
        $this->admin_service = $adminService;
        $this->user_info = auth($this->guard_type)->user();
    }

    /**
     * @expalin 获取全部合格用户
     * @param AdminIndexRequest $adminIndexRequest
     * @return JsonResponse
     * @method
     * @author 陈文小超
     * @maxim from专注to执行
     * @time 2021/3/29/16:39
     */
    public function index(AdminIndexRequest $adminIndexRequest)
    {
        $size = $adminIndexRequest->input('size') ?? config('common.page_size');

        $user_info = $this->admin_service->paginate($size);

        return $this->success(new AdminCollection($user_info));
    }

    /**
     * @expalin 新增用户
     * @param AdminStoreRequest $adminStoreRequest
     * @return JsonResponse
     * @throws AdminException
     * @throws \Throwable
     * @method
     * @author 陈文小超
     * @maxim from专注to执行
     * @time 2021/3/30/10:41
     */
    public function store(AdminStoreRequest $adminStoreRequest)
    {
        try {
            $this->admin_service->store($adminStoreRequest);
        }
        catch ( \Exception $exception ) {
            InstanceLog::instance('admin')->error('创建用户失败:' . $exception->getMessage());

            throw new AdminException(AdminException::ADMIN_ADMIN_CREATE_FAULT);
        }
        $result = [
            'status'  => trans('admin/v1/controller.general.success_status'),
            'message' => trans('admin/v1/controller.admin.success_admin_store'),
        ];

        return $this->success($result);
    }

    /**
     * @expalin 获取指定用户信息
     * @param AdminShowRequest $adminShowRequest
     * @return JsonResponse
     * @throws AdminException
     * @method
     * @author 陈文小超
     * @maxim from专注to执行
     * @time 2021/3/30/11:55
     */
    public function show(AdminShowRequest $adminShowRequest)
    {
        $id = $adminShowRequest->input('id');
        try {
            $user_info = $this->admin_service->find($id);
        }
        catch ( ModelNotFoundException $exception ) {
            InstanceLog::instance('admin')->error('获取用户信息失败：' . $exception->getMessage());

            throw new AdminException(AdminException::ADMIN_ADMIN_RETRIEVE_WITHOUT);
        }
        $result = [
            'data' => new AdminResource($user_info),
        ];

        return $this->success($result);
    }

    /**
     * @expalin 更新用户信息
     * @param AdminUpdateRequest $adminUpdateRequest
     * @return JsonResponse
     * @throws AdminException
     * @method
     * @author 陈文小超
     * @maxim from专注to执行
     * @time 2021/3/30/14:35
     */
    public function update(AdminUpdateRequest $adminUpdateRequest)
    {
        try {
            $admin = $this->admin_service->find($adminUpdateRequest->input('id'));
        }
        catch ( ModelNotFoundException $exception ) {
            InstanceLog::instance('admin')->error('更新用户失败：' . $exception->getMessage());

            throw new AdminException(AdminException::ADMIN_ADMIN_RETRIEVE_WITHOUT);
        }

        if ($admin->isSuper()) {
            throw new AdminException(AdminException::ADMIN_ADMIN_DELETE_INVALID);
        }

        try {
            $this->admin_service->update($admin, $adminUpdateRequest);
        }
        catch ( \Exception $exception ) {
            InstanceLog::instance('admin')->error('更新用户失败：' . $exception->getMessage());

            throw new AdminException(AdminException::ADMIN_ADMIN_SYSTEM_UNKNOWN);
        }
        $result = [
            'status'  => trans('admin/v1/controller.general.success_status'),
            'message' => trans('admin/v1/controller.admin.success_admin_update'),
        ];

        return $this->success($result);
    }

    /**
     * @expalin 删除用户
     * @param AdminDestroyRequest $adminDestroyRequest
     * @return JsonResponse
     * @throws AdminException
     * @method
     * @author 陈文小超
     * @maxim from专注to执行
     * @time 2021/3/30/14:16
     */
    public function destroy(AdminDestroyRequest $adminDestroyRequest)
    {
        try {
            $admin = $this->admin_service->find($adminDestroyRequest->input('id'));
        }
        catch ( ModelNotFoundException $exception ) {
            InstanceLog::instance('admin')->error('删除用户失败：' . $exception->getMessage());

            throw new AdminException(AdminException::ADMIN_ADMIN_RETRIEVE_WITHOUT);
        }

        if ($admin->isSuper()) {
            throw new AdminException(AdminException::ADMIN_ADMIN_DELETE_INVALID);
        }

        try {
            $this->admin_service->destroy($admin);
        }
        catch ( \Exception $exception ) {
            InstanceLog::instance('admin')->error('删除用户失败：' . $exception->getMessage());

            throw new AdminException(AdminException::ADMIN_ADMIN_SYSTEM_UNKNOWN);
        }
        $result = [
            'status'  => trans('admin/v1/controller.general.success_status'),
            'message' => trans('admin/v1/controller.admin.success_admin_delete'),
        ];

        return $this->success($result);
    }

    /**
     * @expalin 我的权限（剔除管理员）
     * @return JsonResponse
     * @method
     * @author 陈文小超
     * @maxim from专注to执行
     * @time 2021/4/1/16:43
     */
    public function permissions()
    {
        $permissions = $this->admin_service->getPermissions($this->user_info);
        $permissions = $permissions->pluck('name');

        return $this->success($permissions);
    }

    /**
     * @expalin 我的菜单（剔除管理员）
     * @return JsonResponse
     * @method
     * @author 陈文小超
     * @maxim from专注to执行
     * @time 2021/4/1/16:52
     */
    public function menus()
    {
        $menus = $this->admin_service->getRoleMenus($this->user_info);

        return $this->success(MenuResource::collection($menus));
    }
}
